local DIY = require "packages/diy_utility/diy_utility"

local kanxian = fk.CreateSkill{
  name = "ls__kanxian",
  tags = {"Phase"}
}

Fk:loadTranslationTable{
  ["ls__kanxian"] = "勘险",
  [":ls__kanxian"] = "阶段技，你可观看一名其他角色的手牌，若类别数与中央区的类别数相同，你视为对其使用【杀】或【顺手牵羊】，否则此回合删除“急袭”{}中的描述。",
  ["#ls__kanxian-prompt"] = "勘险：你可观看一名其他角色的手牌",
  ["#ls__kanxian-choose"] = "勘险：请选择视为对%src使用的牌",
  ["ls__kanxian_slash"] = "使用【杀】",
  ["ls__kanxian_snatch"] = "使用【顺手牵羊】",

  ["$ls__kanxian1"] = "开凿险路，兵行快招。",
  ["$ls__kanxian2"] = "以险取胜，谋划已久。",
}

kanxian:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

local gettypesnum = function (ids)
  local types = {}
  for _, id in ipairs(ids) do
    table.insertIfNeed(types, Fk:getCardById(id).type)
  end
  return #types
end

kanxian:addEffect("active", {
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  max_phase_use_time = 1,
  prompt = "#ls__kanxian-prompt",
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player and not to_select:isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local choices = {"Cancel"}
    if gettypesnum(target:getCardIds("h")) == gettypesnum(DIY.getCenterCards(room)) then
      if player:canUseTo(Fk:cloneCard("slash"), target, {bypass_distances = true, bypass_times = true}) then
        table.insert(choices, #choices, "ls__kanxian_slash")
      end
      if player:canUseTo(Fk:cloneCard("lvshi__snatch"), target, {bypass_distances = true}) then
        table.insert(choices, #choices, "ls__kanxian_snatch")
      end
    end
    if target:isKongcheng() then return end
    local _, choice = room:askToChooseCardsAndChoice(player, {
      cards = target:getCardIds("h"),
      choices = choices,
      skill_name = kanxian.name,
      prompt = "#ls__kanxian-choose:"..target.id,
      min_num = 0,
      max_num = 0,
    })
    if choice == "Cancel" then
      room:addPlayerMark(player, "ls__jixi-turn")
    else
      local pattern = "slash"
      if choice == "ls__kanxian_snatch" then
        pattern = "lvshi__snatch"
      end
      room:useVirtualCard(pattern, nil, player, target, kanxian.name, true)
    end
  end,
})

return kanxian